package com.qf.securitytest;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.TextScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


public class TextSecurityMain {

    private static  String accessKeyId = "LTAI5t7WjWKjtxzcXgVBEW3m";
    private static  String accessKeySecret = "XQpkjMJ7j8I65VayubVhXL7QiwI2Tq";

    //待检测文本内容
//    private static String conent = "我爱中国共产党，共产党万岁!!!";
    private static String conent = "[{\"type\":\"text\",\"value\":\"快乐快乐。。。。。\"},{\"type\":\"image\",\"value\":\"http://localhost:9005/leadnews/2023/02/05/cf78c54737684006b0c04ea75c2d2482.jpg\"},{\"type\":\"text\",\"value\":\"请在这里输入正文\"}]";

    public static void main(String[] args) throws Exception {
        IClientProfile profile = DefaultProfile
                .getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        DefaultProfile
                .addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        TextScanRequest textScanRequest = new TextScanRequest();
        textScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
        textScanRequest.setHttpContentType(FormatType.JSON);
        textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
        textScanRequest.setEncoding("UTF-8");
        textScanRequest.setRegionId("cn-shanghai");

        //任务集，可以同时检测多个文本任务
        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        //具体待检测的任务
        Map<String, Object> task1 = new LinkedHashMap<String, Object>();
        task1.put("dataId", UUID.randomUUID().toString());
        /**
         * 待检测的文本，长度不超过10000个字符。
         */
        task1.put("content", conent);
        tasks.add(task1);

        JSONObject data = new JSONObject();

        /**
         * 检测场景。文本垃圾检测请传递antispam。
         **/
        data.put("scenes", Arrays.asList("antispam"));
        data.put("tasks", tasks);

        System.out.println(JSON.toJSONString(data));
        textScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
        // 请务必设置超时时间。
        textScanRequest.setConnectTimeout(3000);
        textScanRequest.setReadTimeout(6000);
        try {
            //发起检测
            HttpResponse httpResponse = client.doAction(textScanRequest);

            if (!httpResponse.isSuccess()) {
                //检测服务器出现了问题
                System.out.println("response not success. status:" + httpResponse.getStatus());
                // 业务处理。
                return;
            }

            JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
            System.out.println(JSON.toJSONString(scrResponse));
            if (200 != scrResponse.getInteger("code")) {
                //检测服务器出现了问题
                System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                // 业务处理。
                return;
            }

            //获取检测服务返回的多个任务结果
            JSONArray taskResults = scrResponse.getJSONArray("data");
            for (Object taskResult : taskResults) {
                //针对每一个任务结果做解析处理
                if (200 != ((JSONObject) taskResult).getInteger("code")) {
                    //检测服务器出现了问题
                    System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
                    // 业务处理。
                    continue;
                }

                //获取当前检测任务的多个场景对应的结果
                JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
                for (Object sceneResult : sceneResults) {
                    //针对该任务的每个场景结果进行解析
                    String scene = ((JSONObject) sceneResult).getString("scene");
                    String suggestion = ((JSONObject) sceneResult).getString("suggestion");
                    // 根据scene和suggestion做相关处理。
                    // suggestion为pass表示未命中垃圾。suggestion为block表示命中了垃圾，可以通过label字段查看命中的垃圾分类。
                    System.out.println("args = [" + scene + "]");
                    System.out.println("args = [" + suggestion + "]");
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
